package smedi.ismedi.mobile.bff.controller.personnel;

import com.smedi.ismedi.personnel.api.dto.response.ProfileImageResponse;
import com.smedi.ismedi.personnel.api.dto.response.SignatureResponse;
import com.smedi.ismedi.personnel.api.feign.RemotePersonAssistFeignApi;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.exception.LogicException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import smedi.ismedi.mobile.bff.controller.BaseController;
import smedi.ismedi.mobile.bff.infrastructure.constant.CustomError;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * 移动端人员管理文件BFF
 *
 * @author xulinjun
 */
@Slf4j
@RestController
@Api(tags = {"移动端人员管理文件接口"})
@RequestMapping("/mobilebff/v1/personnelAssist")
@AllArgsConstructor
public class PersonAssistController extends BaseController {
    private final RemotePersonAssistFeignApi remotePersonAssistFeignApi;

    /**
     * 下载文件输出
     *
     * @param httpServletResponse
     * @param fileName
     * @param bytes
     * @throws IOException
     */
    private void writeHttpServletResponse(HttpServletResponse httpServletResponse, String fileName, byte[] bytes) throws IOException {
        httpServletResponse.reset();
        httpServletResponse.setContentType("application/x-download");
        httpServletResponse.addHeader("Content-Disposition", "attachment;filename=\"" + URLEncoder.encode(fileName, "UTF-8") + "\"");
        httpServletResponse.getOutputStream().write(bytes);
        httpServletResponse.getOutputStream().close();
    }


    /**
     * 下载人员头像照片
     *
     * @param imageUUID
     */
    @ApiOperation(httpMethod = "GET", value = "下载人员头像照片")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "imageUUID", value = "照片id", dataType = "String", dataTypeClass = String.class)
    })
    @GetMapping(value = "/downloadProfileImage")
    public ResponseData<ProfileImageResponse> getDownloadProfileImage(@RequestParam("imageUUID") String imageUUID){
       return remotePersonAssistFeignApi.getDownloadProfileImage(imageUUID);
    }

    /**
     * 上传人员头像照片，并且不需要确认即可保存
     *
     * @param file
     * @param personId
     * @return
     */
    @ApiOperation(httpMethod = "POST", value = "上传人员头像照片，并且不需要确认即可保存")
    @PostMapping(value = "/uploadProfileImageWithNoConfirm", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseData<Boolean> postUploadProfileImageWithNoConfirm(@RequestPart("file") MultipartFile file, @RequestParam("personId") Long personId) {
        return remotePersonAssistFeignApi.postUploadProfileImageWithNoConfirm(file, personId);
    }

    /**
     * 下载人员签章
     *
     * @param signatureUUID
     */
    @ApiOperation(httpMethod = "GET", value = "下载人员签章")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "signatureUUID", value = "照片id", dataType = "String", dataTypeClass = String.class)
    })
    @GetMapping(value = "/downloadSignature")
    public void getDownloadSignature(@RequestParam("signatureUUID") String signatureUUID, HttpServletResponse httpServletResponse) throws IOException {
        ResponseData<SignatureResponse> responseData = remotePersonAssistFeignApi.getDownloadSignature(signatureUUID);
        if (responseData.isSuccess()) {
            writeHttpServletResponse(httpServletResponse, responseData.getData().getFileName(), responseData.getData().getBytes());
        } else {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, responseData.getApplication(), String.valueOf(responseData.getErrorMsg()));
        }
    }

    /**
     * 根据人员id下载人员签章
     *
     * @param personId
     */
    @ApiOperation(httpMethod = "GET", value = "根据人员id下载人员签章")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "personId", value = "人员id", dataType = "Long")
    })
    @GetMapping(value = "/downloadSignatureByPersonId")
    public void getDownloadSignatureByPersonId(@RequestParam("personId") Long personId, HttpServletResponse httpServletResponse) throws IOException {
        ResponseData<SignatureResponse> responseData = remotePersonAssistFeignApi.getDownloadSignatureByPersonId(personId);
        if (responseData.isSuccess()) {
            writeHttpServletResponse(httpServletResponse, responseData.getData().getFileName(), responseData.getData().getBytes());
        } else {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, responseData.getApplication(), String.valueOf(responseData.getErrorMsg()));
        }
    }

    /**
     * 上传人员签章，并且不需要确认即可保存（不含校验文件格式、文件大小功能）
     *
     * @param file
     * @param personId
     * @return
     */
    @ApiOperation(httpMethod = "POST", value = "上传人员签章，并且不需要确认即可保存（不含校验文件格式、文件大小功能）", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @PostMapping(value = "/uploadSignatureWithNoConfirm", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseData<Boolean> postUploadSignatureWithNoConfirm(@RequestPart("file") MultipartFile file, @RequestParam("personId") Long personId) {
        return remotePersonAssistFeignApi.postUploadSignatureWithNoConfirm(file, personId);
    }
}
